package com.niit.bc.servlet;

import com.niit.bc.bean.CartBean;
import com.niit.bc.util.web.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CartBook extends HttpServlet {

    /**
     * @潘玉
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/xml");
        response.setCharacterEncoding("utf8");
        request.setCharacterEncoding("utf8");

        String s_bookId = request.getParameter("bookId");
        String s_bookCount = request.getParameter("bookCount");
        String op = request.getParameter("op");// 取得操作购物车的方式

        int bookId = 0;
        int bookCount = 0;
        if (s_bookId != null && !"".equals(s_bookId)) {
            bookId = Integer.parseInt(s_bookId);
        }
        if (s_bookCount != null && !"".equals(s_bookCount)) {
            bookCount = Integer.parseInt(s_bookCount);
        }

        System.out.println(op);
        System.out.println(bookId);
        System.out.println(bookCount);

        int type = 0;
        if ("add".equals(op)) {
            CartBean cartBean = (CartBean) request.getSession().getAttribute(Constants.BOOK_SESSION_CART);
            if (cartBean == null) {
                // 第一次购物，创建一个购物车，同时加入session
                cartBean = new CartBean();
                request.getSession().setAttribute(Constants.BOOK_SESSION_CART, cartBean);
            }
            if (bookId != 0 && bookCount != 0) {
                cartBean.addBooks(bookId, bookCount);
            }
            /*
             * int count = 0; if (cartBean.myCart.containsKey(bookId)) {//取得
			 * 购物车中存在此商品的个数 count = ((Integer)
			 * cartBean.myCart.get(bookId)).intValue(); }
			 */

            type = cartBean.myCart.size();

        } else if ("minus".equals(op)) {
            CartBean cartBean = (CartBean) request.getSession().getAttribute(Constants.BOOK_SESSION_CART);
            if (cartBean.myCart.containsKey(bookId)) {
                cartBean.minusBooks(bookId, bookCount);
                type = cartBean.myCart.size();
            } else {
                type = cartBean.myCart.size();
            }
        } else if ("delete".equals(op)) {
            CartBean cartBean = (CartBean) request.getSession().getAttribute(Constants.BOOK_SESSION_CART);
            if (bookId != 0 && bookCount != 0) {
                cartBean.deleteBooks(bookId);
                type = cartBean.myCart.size();
            }
        } else if ("deleteAll".equals(op)) {
            CartBean cartBean = (CartBean) request.getSession().getAttribute(Constants.BOOK_SESSION_CART);
            boolean flag = cartBean.deleteAllBooks();
            if (flag) {
                type = 0;
            }
        }

        PrintWriter out = response.getWriter();
        out.println("<response>");
        out.println("<cart>");
        out.println("<bookCount>");
        out.println(type);
        out.println("</bookCount>");
        out.println("</cart>");
        out.println("</response>");
        out.flush();
        out.close();

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

}
